<template>
  <div class="login">
    <el-form class="login-form" :model="loginForm" :rules="loginRules" ref="loginForm">
      <h3 class="title">若依后台管理系统</h3>
      <el-form-item prop="userName">
        <el-input type="text" placeholder="账号" v-model="loginForm.userName"></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input type="password" placeholder="密码" v-model="loginForm.password"></el-input>
      </el-form-item>
      <el-form-item prop="code">
        <el-input placeholder="验证码" v-model="loginForm.code" style="width:63%;"></el-input>
        <div class="login-code">
          <img :src="codeUrl" class="login-code-img" @click="getCode">
        </div>
      </el-form-item>
      <el-checkbox v-model="loginForm.remember" style="margin: 0px 0px 25px 0px;">记住密码</el-checkbox>
      <el-form-item style="width: 100%;">
        <el-button @click.native="handleLogin" type="primary" style="width: 100%;">
          <span>登录</span>
        </el-button>
      </el-form-item>
    </el-form>
    <div class="login-footer">
      <span>Copyright © 2018-2024 ruoyi.vip All Rights Reserved.</span>
    </div>
  </div>
</template>

<script>
import { getCodeImg, login } from '../api/login'

export default {
  name: 'userLogin',
  data () {
    return {
      loginForm: {
        userName: '',
        password: '',
        code: '',
        uuid: '',
        remember: ''
      },
      codeUrl: '',
      captcharOnOff: true,
      loginRules: {
        userName: [
          { required: true, trigger: 'blur', message: '请输入用户名' }
        ],
        password: [
          { required: true, trigger: 'blur', message: '请输入密码' }
        ],
        code: [
          { required: true, trigger: 'change', message: '请输入验证码' }
        ]
      }
    }
  },
  created () {
    this.getCode()
  },
  methods: {
    getCode () {
      getCodeImg().then(res => {
        this.captcharOnOff = res.captchaEnabled === undefined ? true : res.captchaEnabled
        if (this.captcharOnOff) {
          this.codeUrl = 'data:image/gif;base64,' + res.img
          this.loginForm.uuid = res.uuid
        }
      })
    },
    handleLogin () {
      this.$refs.loginForm.validate(validate => {
        if (validate) {
          const userName = this.loginForm.userName
          const password = this.loginForm.password
          const code = this.loginForm.code
          const uuid = this.loginForm.uuid
          login(userName, password, code, uuid).then(res => {
            console.log('login:', res)
          })
        }
      })
    }
  }
}

</script>

<style scoped lang="scss">
.login {
  display: flex;
  background-image: url("../assets/login-background.jpg");
  height: 100%;
  background-size: cover;
  justify-content: center;
  align-items: center;
}

.login-form {
  box-sizing: border-box;
  width: 400px;
  border-radius: 6px;
  background: #fff;
  padding: 25px 25px 5px 25px;
}

.login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0px;
  color: #fff;
  font-size: 12px;
  letter-spacing: 1px;
  font-family: Arial;
}

.title {
  color: #707070;
  text-align: center;
  margin: 0px auto 30px;
}

.login-code {
  width: 33%;
  float: right;

  img {
    cursor: pointer;
  }
}

.login-code-img {
  height: 40px;
}
</style>
